set(LOG_SRCS
	logging.cpp
	logger.cpp
	log_stream.cpp
	internal/log_manager.cpp
	slog2/slog2.cpp
	slog2/slog2_context.cpp
	slog2/slog2_context_data.cpp
)
set(nosal_log_library_name nosal_${PROJECT_NAME})

add_library(${PROJECT_NAME} ${LOG_SRCS})

target_include_directories(${PROJECT_NAME}
	PUBLIC
		$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
		$<INSTALL_INTERFACE:include>
	PRIVATE
		$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>
)

target_link_libraries(${PROJECT_NAME}
	PUBLIC
		nosal::core
		slog2
		#Threads::Threads
)
set_target_properties(${PROJECT_NAME} PROPERTIES
	OUTPUT_NAME ${nosal_log_library_name}
	POSITION_INDEPENDENT_CODE ON
)

add_library(nosal::log ALIAS ${PROJECT_NAME})

# -----------------------------

set(nosal_log_export_name "nosal-${PROJECT_NAME}")

include(FindPkgConfig)
if(PKG_CONFIG_FOUND)
	configure_file(${PROJECT_SOURCE_DIR}/files/pkgconfig/${nosal_log_export_name}.pc.in ${nosal_log_export_name}.pc @ONLY)
	install(
		FILES ${CMAKE_CURRENT_BINARY_DIR}/${nosal_log_export_name}.pc
		DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
	)
endif()

# Generate *-config.cmake file
include(CMakePackageConfigHelpers)

configure_package_config_file(
	${PROJECT_SOURCE_DIR}/files/cmake/${nosal_log_export_name}-config.cmake.in
	${CMAKE_CURRENT_BINARY_DIR}/${nosal_log_export_name}-config.cmake
	INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${nosal_log_export_name}
	NO_SET_AND_CHECK_MACRO # nosal::log does not set any path variables
)
# Generate *-config-version.cmake file
write_basic_package_version_file(
	${CMAKE_CURRENT_BINARY_DIR}/${nosal_log_export_name}-config-version.cmake
	COMPATIBILITY ExactVersion
)
# Install generated *-config.cmake and *-version.cmake files
install(
	FILES
		${CMAKE_CURRENT_BINARY_DIR}/${nosal_log_export_name}-config.cmake
		${CMAKE_CURRENT_BINARY_DIR}/${nosal_log_export_name}-config-version.cmake
	DESTINATION
		${CMAKE_INSTALL_LIBDIR}/cmake/${nosal_log_export_name}
)

# Install the actual target(s), i.e. the libraries and executables
install(
	TARGETS ${PROJECT_NAME}
	EXPORT ${nosal_log_export_name}-targets
	ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
	LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
	RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
# Install exported targets, i.e. *-targets.cmake
install(
	EXPORT ${nosal_log_export_name}-targets
	DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${nosal_log_export_name}
	NAMESPACE nosal::
)
